

# 线程之间是共享内存的

#coding=utf-8

import threading

import time

import os

count = 10

def modifycount(nums):

    #获取当前线程对象

    t=threading.current_thread()

    for index in range(count):

        nums.append(index)

        print('%s,修改nums'%(t.name))

        time.sleep(0.1)

def printcount(nums):

    #获取当前线程对象

    t=threading.current_thread()

    for index in range(count):

        print('%s,nums=%s'%(t.name,nums))

        time.sleep(0.1)

if __name__ == "__main__":

    print('pid=%d'%os.getpid())

    nums=[]

    #创建线程，此线程修改全局变量

    t=threading.Thread(target=modifycount,args=(nums,))

    #创建线程，此线程打印全局变量

    t2=threading.Thread(target=printcount,args=(nums,))

    t.start()

    t2.start()

    t.join()

    t2.join()

    print('主线程结束,nums=%s'%(nums))